package com.example.function_interface.operator;

import com.example.function_interface.User;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.function.IntUnaryOperator;

/**
 * 接受一个参数同为类型int,返回值类型也为int 。
 */
public class IntUnaryOperatorTest {
    @Test
    public void myTest() throws Exception{
        List<User> userList = new ArrayList<>();
        User user1 = new User(1, "张三", 12, false, 23.8);
        User user2 = new User(2, "李四", 22, false, 200);
        User user3 = new User(3, "王五", 32, true, 123.2);
        User user4 = new User(4, "郑静", 2, true, 0);
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);

        IntUnaryOperator intUnaryOperator = new IntUnaryOperator() {
            @Override
            public int applyAsInt(int operand) {
                return userList.get(operand-1).getAge();
            }
        };
        IntUnaryOperator intUnaryOperator1 = intUnaryOperator.compose(operand -> operand-1);
        IntUnaryOperator intUnaryOperator2 = intUnaryOperator.andThen(operand -> operand / 2);

        System.out.println(intUnaryOperator1.applyAsInt(2));
        System.out.println(intUnaryOperator2.applyAsInt(2));
    }
}
